package com.example.park2212.sign

import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.example.lib_base.entity.SignBean
import com.example.lib_base.entity.SignBeanItem
import com.example.lib_base.entity.VisitStatusBean
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.Const
import com.example.park2212.databinding.ActivitySignListBinding


import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar

@AndroidEntryPoint
@Route(path = "/app/sign/SignListActivity")
class SignListActivity : BaseActivity<ActivitySignListBinding,SignViewModel>() {
    val signStatusAdapter by lazy { SignStatusAdapter() }
    val signAdapter by lazy { SignAdapter() }
    lateinit var signs:List<SignBeanItem>//已经查出某月的集合
    var month = SimpleDateFormat("MM").format(System.currentTimeMillis())
    var singsByMonth = SignBean()//根据月份查询的原集合

    var clickNum = 0
    val calendar = Calendar.getInstance()
    override fun initView() {
        binding.headSign.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("考勤管理")
            headerAdd.visibility = View.GONE
        }


        val list = listOf<VisitStatusBean>(VisitStatusBean("全部",true),
            VisitStatusBean("正常",false),
            VisitStatusBean("迟到",false),
        VisitStatusBean("早退",false))
        signStatusAdapter.addData(list)
        binding.signRvstatus.apply {
            adapter = signStatusAdapter
            layoutManager = LinearLayoutManager(this@SignListActivity,RecyclerView.HORIZONTAL,false)
        }

        signStatusAdapter.setOnItemClickListener { adapter, view, position ->
            signStatusAdapter.data.forEach {
                it.isCheck = false
            }
            signStatusAdapter.data.get(position).isCheck = true
            signStatusAdapter.notifyDataSetChanged()

            signAdapter.data.clear()
            if (position == 0){
                signAdapter.addData(signs)
            }else{
                signAdapter.addData(signs.filter {
                    it.status == signStatusAdapter.data.get(position).status
                })
            }
            signAdapter.notifyDataSetChanged()
        }

        //viewModel.getSignList(SPUtils.getInstance().getInt(Const.UID))
        viewModel.getSignList(306)
        binding.rvSign.apply {
            adapter = signAdapter
            layoutManager = LinearLayoutManager(this@SignListActivity)
        }

        binding.signSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                signAdapter.data.clear()
                signAdapter.addData(signs.filter {
                    if (it.morningCheckIn.isNullOrEmpty())
                        it.afternoonCheckIn.split(" ")[1].contains(newText!!)
                    else
                        it.morningCheckIn.split(" ")[1].contains(newText!!)
                })
                signAdapter.notifyDataSetChanged()
                return true
            }
        })

        binding.signMonth.setText(month)


        binding.sgnNext.setOnClickListener {
            //clickNum++
            selectByMonth(1)
        }
        binding.sgnPre.setOnClickListener {
            //clickNum--
            selectByMonth(-1)
        }

    }
    fun selectByMonth(clickNum: Int) {
        calendar.add(Calendar.MONTH, clickNum)
        val newMonth = SimpleDateFormat("MM").format(calendar.time)
        binding.signMonth.setText(newMonth)

        signAdapter.data.clear()
        signs = singsByMonth.filter {
            val afternoonCheckIn = it.afternoonCheckIn
            val morningCheckIn = it.morningCheckIn

            if (morningCheckIn.isNullOrEmpty()) {
                afternoonCheckIn != null &&
                        afternoonCheckIn.split("-").size >= 2 &&
                        afternoonCheckIn.split("-")[1] == newMonth
            } else {
                morningCheckIn != null &&
                        morningCheckIn.split("-").size >= 2 &&
                        morningCheckIn.split("-")[1] == newMonth
            }
        }
        signAdapter.addData(signs)
        signAdapter.notifyDataSetChanged()
    }

    override fun initData() {
        viewModel.getSignSuccess.observe(this) {
            singsByMonth = it
            it.forEach {
                if (it.morningCheckIn.isNullOrEmpty()) {
                    it.type = 1
                } else {
                    it.type = 0
                }
            }
            signs = it.filter {
                val afternoonCheckIn = it.afternoonCheckIn
                val morningCheckIn = it.morningCheckIn

                if (morningCheckIn.isNullOrEmpty()) {
                    afternoonCheckIn != null && afternoonCheckIn.split("-").size >= 2 && afternoonCheckIn.split("-")[1] == month
                } else {
                    morningCheckIn != null && morningCheckIn.split("-").size >= 2 && morningCheckIn.split("-")[1] == month
                }
            }
            signAdapter.addData(signs)
            signAdapter.notifyDataSetChanged()
        }
    }

}